package com.lanou.pianke.piece.web.controller;
import com.lanou.pianke.piece.web.entity.Piece;
import com.lanou.pianke.piece.web.pieceService.impl.PieceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class PieceController {

    @Autowired
    private PieceService pieceService;

    //显示所有碎片
    @ResponseBody
    @GetMapping("/piece/allPiecetag")
    public List<Piece> showAllTagPiece(@RequestParam(defaultValue = "1") int pageNum,
                                       @RequestParam(defaultValue = "10") int pageSize) {
        List<Piece> allPieceList = pieceService.selectAllPiece(pageNum,pageSize);
        return allPieceList;
    }


    //显示单个种类碎片
    @ResponseBody
    @GetMapping("/piece/onePiecetag")
    public List<Piece> showOneTagPiece(Integer tagId) {
        List<Piece> onePieceList = pieceService.selectOneTagPieceByTagid(tagId);
        return onePieceList;
    }


    //发布碎片
    @ResponseBody
    @GetMapping("/piece/addPiece")
    public void addPiece(Piece piece){
        pieceService.addPiece(piece);
    }


    //进入单个碎片
    @ResponseBody
    @GetMapping("/piece/goOnePiece")
    public void goOnePiece(Map map,String pieceId,Long uid){
        Integer integer = pieceService.selectOnePieceByPid(pieceId, uid);
        if (integer.equals(0)) {
            map.put("OnePieceShow", "删除");
        }else {
            map.put("OnePieceShow", "举报");
        }
        Piece piece = pieceService.selectOnePieceByPieceId(pieceId);
        map.put("PieceUid",uid);
        map.put("ShowPiece",piece);
    }


    //相关碎片推荐
    @ResponseBody
    @GetMapping("/pianke/pieceRecommend")
    public void recommend(){
        pieceService.selectPiece();
    }

    //碎片是否喜欢
    @ResponseBody
    @GetMapping("/pianke/likePiece")
    public void addLikePiece(String pieceId,Long uid){
        pieceService.insertPieceLike(pieceId,uid);
    }




    //存储到Redis
    @ResponseBody
    @GetMapping("/pianke/addRedis")
    public void addRedis(){
        pieceService.addRedis();
    }

    //获取
    @ResponseBody
    @GetMapping("/pianke/getRedis")
    public void getRedis(){
        pieceService.getRedis();
    }

}
